# -*- coding: utf-8-*-
from openerp.osv import osv, fields
from weixin_sync_operation import WXSyncOperation
from openerp.addons.dftg_weixin.application.application_weixin_contact import WxAppContact

# 微信企业号应用
class wechat_department(osv.osv):
    _name = 'wechat.department'
    _description = u"微信应用"

    _order = 'order'

    _columns = {
        'name': fields.char(u'名称'),
        'parentid': fields.integer(u'父部门id'),
        'order': fields.integer(u'次序值'),
        'department_id': fields.integer(u'部门id'),
        'enterprise_id': fields.many2one('wechat.enterprise', u'企业号'),
        'system_department_id': fields.many2one('hr.department', u'人力资源中部门'),
    }
    _defaults = {
        'parentid': 1
    }


class wechat_enterprise(osv.osv):
    _inherit = 'wechat.enterprise'

    def sync_department(self, cr, uid, ids, context=None):
        weixin_contact = WxAppContact()
        return_vals = weixin_contact.wxapi.get_all_department()
        wechat_department_dict = {}
        if return_vals and return_vals[0].get('errcode', 1) == 0:
            wechat_department_dict = {depatment.get('id'):depatment for depatment in return_vals[0].get('department', 1)}
        for enterprise in self.browse(cr, uid, ids, context=context):
            local_department_ids = []
            for department in enterprise.department_ids:
                local_department_ids.append(department.department_id)
                res = {
                    'name': department.name,
                    'parentid': department.parentid,
                    'order': department.order,
                    'id': department.department_id,
                }
                if department.department_id in wechat_department_dict:
                    if department.department_id != 1:
                        weixin_contact.wxapi.update_department(res)
                else:
                    weixin_contact.wxapi.create_department(res)
            department_difference_set = list(set(wechat_department_dict.keys()) - set(local_department_ids))
            if department_difference_set:
                for department_id in department_difference_set:
                    weixin_contact.wxapi.delete_department({'id': department_id})

    def sync_weixin_department(self, cr, uid, ids, context=None):
        weixin_contact = WxAppContact()
        department_obj = self.pool.get('wechat.department')
        return_vals = weixin_contact.wxapi.get_all_department()
        if return_vals and return_vals[0].get('errcode', 1) == 0:
            for depatment in return_vals[0].get('department', 1):
                department_ids = department_obj.search(cr, uid, [('department_id', '=', depatment.get('id'))], context=context)
                if department_ids:
                    department_obj.unlink(cr, uid, department_ids, context=context)
                depatment.update({'department_id': depatment.get('id'), 'enterprise_id': ids[0]})
                department_obj.create(cr, uid, depatment, context=context)
        else:
            raise osv.except_osv(u'错误', return_vals[0].get('errcode', 1))


    _columns = {
        'department_ids': fields.one2many('wechat.department', 'enterprise_id', u'组织架构'),
    }
